/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package sheepmixView.server;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;


import sheepmixView.client.PlayingCommand;
import sheepmixView.client.model.Musique;
import clientConnexion.Client;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class PlayingCommandImpl extends RemoteServiceServlet implements PlayingCommand {

	
	/**
	 * 
	 */
	
	public static Musique playedMusic;
	public static LinkedList<Musique> playlist = new LinkedList<Musique>();
	private static final long serialVersionUID = 1L;
	public static Client client = new Client();
	private static String    cheminPochetteGenre = "genres";
	public static boolean updateMusique = false;
	public static boolean updatePlaylist = false;
	
	@Override
	public void play() {
		System.out.println("Play");
		client.ev_play();
	}

	@Override
	public void stop() {
		client.ev_stop();
	}

	@Override
	public boolean pass() {
		return client.ev_pass();
	}

	@Override
	public void load() {
		client.ev_load();
	}

	@Override
	public void connect() {
		client.start();
		
	}

	@Override
	public LinkedList<String> getGenre() {
		return client.ev_getGenre();
	}


	@Override
	public String getImage(String genre) {
		
		File pochettes = new File(cheminPochetteGenre);
		try {
			System.out.println("pochettes : " + pochettes.getCanonicalPath());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File[] nomsPochette = pochettes.listFiles();
		for (int i = 0; i < nomsPochette.length; i++) {
			if(nomsPochette[i].getName().toLowerCase().replace('-', ' ').contains(genre.toLowerCase().replace('-', ' ')) || 
					genre.toLowerCase().replace('-', ' ').contains(nomsPochette[i].getName().toLowerCase().replace('-', ' '))) {
				return nomsPochette[i].getName();
			}
		}
		return "noteMusique.png";
	}

	@Override
	public Musique getCurrentMusique() {
		if (updateMusique) {
			updateMusique = false;
			return playedMusic;
		}
		else
			return null;
	}

	@Override
	public LinkedList<Musique> getCurrentPlaylist() {
		if(updatePlaylist) {
			updatePlaylist = false;
			return playlist;
		}
		else
			return null;
	}

	@Override
	public void loadGenres(LinkedList<String> genres) {
		System.out.println("Load genre client side");
		client.ev_load_genre(genres);
		
	}

	@Override
	public void upVolume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void downVolume() {
		// TODO Auto-generated method stub
		
	}
}
